package LocalCRM::Doc::DestList;

use base 'LocalCRM::Doc';
use Encode;
use Data::Dumper;

sub new
{
    my $self = shift->SUPER::new();
    my $x, $y;

    $self->{page} = $self->{pdf}->page();
    $self->{page}->mediabox(842.4,597.6);

    my $border	= $self->{page}->gfx();
    my $text	= $self->{page}->text();

    $border->rectxy((18/2.54*72),(2/2.54*72),(27/2.54*72),(6.5/2.54*72));
    $border->linedash(1,3);
    $border->stroke;

#    $text->translate((18.1/2.54*72),(3.6/2.54*72));
#    $text->text(encode('windows-1251',decode_utf8('Адрес:')));

    return $self;
}

sub setData
{
    my $self = shift;
    my %arg  = @_;

    my $text = $self->{page}->text();

    $text->lead(10);

    if ( $arg{fromName} )
    {
	$text->translate((18.1/2.54*72),(6/2.54*72));
	$text->font($self->{font}->{Verdana},10);
	$text->text(encode('windows-1251',decode_utf8('От:')));

	$text->translate((19.5/2.54*72),(6/2.54*72));
	$text->font($self->{font}->{Verdana},8);
	$text->text(encode('windows-1251',decode_utf8($arg{fromName})));
    }

    if ( $arg{toName} )
    {
	$text->translate((18.1/2.54*72),(5.5/2.54*72));
	$text->font($self->{font}->{Verdana},10);
	$text->text(encode('windows-1251',decode_utf8('Кому:')));

	$text->translate((19.5/2.54*72),(5.5/2.54*72));
	$text->font($self->{font}->{Verdana},8);
	$text->paragraph(encode('windows-1251',decode_utf8($arg{toName})),(5.5/2.54*72),(2/2.54*72));
    }

    if ( $arg{contractNum} )
    {
	my ( $x, $y ) = $text->textpos();

	$text->translate((18.1/2.54*72),$y-5);
	$text->font($self->{font}->{Verdana},10);
	$text->text(encode('windows-1251',decode_utf8('Договор:')));

	$text->translate((20/2.54*72),$y-5);
	$text->font($self->{font}->{Verdana},8);
	$text->text(encode('windows-1251',decode_utf8($arg{contractNum})));
    }

    if ( $arg{opt} )
    {
	my ( $x, $y ) = $text->textpos();

	foreach my $field_name ( keys(%{$arg{opt}}) )
	{
	    $text->translate(18.1/2.54*72,$y-12);
	    $text->font($self->{font}->{Verdana},10);
	    $text->text($self->utf2win($field_name).(($field_name!~/:$/)?':':undef));

	    ($x,$y) = $text->textpos();
	    $text->translate($x+5,$y);
	    $text->font($self->{font}->{Verdana},8);

	    if ( ref $arg{opt}->{$field_name} eq 'ARRAY' )
	    {
		$text->paragraph($self->utf2win(join ', ',@{$arg{opt}->{$field_name}}),(6/2.54*72),(2/2.54*72));
	    }
	    else
	    {
		$text->paragraph($self->utf2win($arg{opt}->{$field_name}),(6/2.54*72),(2/2.54*72));
	    }
	}
    }

    return $self;
}

1;
